package com.gccloud.tgyfirst.service.demo.service.impl;

import com.gccloud.tgyfirst.service.demo.dao.CourseDao;
import com.gccloud.tgyfirst.demo.entity.CourseEntity;
import com.gccloud.tgyfirst.demo.entity.StudentCourseEntity;
import com.gccloud.tgyfirst.service.demo.service.ICourseService;
import com.gccloud.tgyfirst.service.demo.service.IStudentCourseService;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gccloud.starter.common.dto.SearchDTO;
import com.gccloud.starter.common.exception.GlobalException;
import com.gccloud.starter.core.shiro.SysUser;
import com.gccloud.starter.core.shiro.UserUtils;
import com.gccloud.starter.mybatis.page.PageVO;
import com.gccloud.starter.mybatis.utils.QueryWrapperUtils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.MessageFormat;

/**
 * @author qianxing
 * @version 1.0
 * @date 2021/5/17 14:57
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseDao, CourseEntity> implements ICourseService {

    @Lazy
    @Resource
    private IStudentCourseService studentCourseService;

    @Override
    public PageVO<CourseEntity> getPage(SearchDTO searchDTO) {
        LambdaQueryWrapper<CourseEntity> queryWrapper = QueryWrapperUtils.wrapperLike(new LambdaQueryWrapper(), searchDTO.getSearchKey(), CourseEntity::getName, CourseEntity::getTeacherName);
        queryWrapper.orderByDesc(CourseEntity::getUpdateDate);
        return this.page(searchDTO, queryWrapper);
    }

    @Override
    public PageVO<CourseEntity> getMyCourse(SearchDTO searchDTO){
        Page<CourseEntity> page = new Page<>(searchDTO.getCurrent(), searchDTO.getSize());
        /**
         * UserUtils是用户工具类，里面有很多方法，比如获取当前登录用户
         */
        SysUser currentUser = UserUtils.getCurrentUser(false);
        IPage<CourseEntity> listPage =this.baseMapper.getMyCourse(page, currentUser.getId());
        return new PageVO<>(listPage);
    }

    @Override
    public void add(CourseEntity courseEntity) {
        if (checkRepeat(courseEntity)){
            throw new GlobalException(MessageFormat.format("该老师【{0}】的课程【{1}】已存在", courseEntity.getTeacherName(), courseEntity.getName()));
        }
        this.save(courseEntity);
    }

    @Override
    public void update(CourseEntity courseEntity) {
        if (checkRepeat(courseEntity)){
            throw new GlobalException(MessageFormat.format("该老师【{0}】的课程【{1}】已存在", courseEntity.getTeacherName(), courseEntity.getName()));
        }
        this.updateByIdWithDp(courseEntity);
    }

    @Override
    public void deleteByCourseId(String id) {
        // 删除已选
        LambdaQueryWrapper<StudentCourseEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StudentCourseEntity::getCourseId, id);
        studentCourseService.delete(queryWrapper);
        this.deleteByIdWithDp(id);
    }

    private boolean checkRepeat(CourseEntity courseEntity){
        LambdaQueryWrapper<CourseEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseEntity::getName, courseEntity.getName());
        queryWrapper.eq(CourseEntity::getTeacherName, courseEntity.getTeacherName());
        if (StringUtils.isNotBlank(courseEntity.getId())){
            queryWrapper.ne(CourseEntity::getId, courseEntity.getId());
        }
        return count(queryWrapper) > 0;
    }

}
